package com.flyme.module.file.provider.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.constants.BaseConstants;
import com.flyme.module.file.client.entity.SysCourseComment;
import com.flyme.module.file.provider.mapper.SysCourseCommentMapper;
import com.flyme.module.file.provider.service.SysCourseCommentService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: flyme
 * @date: 2018/3/7 15:27
 * @desc: 课程评论接口实现
 */
@Service
public class SysCourseCommentServiceImpl extends BaseServiceImpl<SysCourseCommentMapper, SysCourseComment> implements SysCourseCommentService {

    /**
     *	根据目标查询对应课程的一级评论总数
     */
    public Integer firstCommentNum(Long courseId){
        QueryWrapper queryWrapper = new QueryWrapper<SysCourseComment>();
        queryWrapper.eq("targetId", courseId);
        queryWrapper.eq("replyType", 1);
        return count(queryWrapper);
    }

    /**
     * 根据目标查询对应的评论
     */
    public List<EntityMap> selectByCourse(String targetId,PagerInfo pagerInfo,Integer replyType){
        pagerInfo.setPageSize(7);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("cc.id,cc.commentContent,cc.replyUserId,cc.createTime,"
                + "CASE WHEN ISNULL(u.nickName) THEN '\u6e38\u5ba2' ELSE u.nickName END nickName,"
                + "CASE WHEN ISNULL(u.avatar) THEN '"+ BaseConstants.DEFAULT_USERHEAD +"' ELSE u.avatar END avatar");
        queryWrapper.eq("targetId", targetId);
        queryWrapper.eq("replyType", replyType);
        queryWrapper.orderByDesc("cc.createTime");
        queryWrapper.createAlias("user", "u");
        List<EntityMap> list = (List<EntityMap>) pageList(queryWrapper).getRows();
        return list;
    }


    /**
     * 根据目标查询对应的评论2
     */
    public List<EntityMap> selectByCourse2(String targetId,PagerInfo pagerInfo,Integer replyType,String accountId){
        pagerInfo.setPageSize(100);
        String sql = "cc.id,cc.commentContent,cc.replyUserId,cc.createTime,CASE WHEN ISNULL(u.nickName) THEN '\u6e38\u5ba2' ELSE u.nickName END nickName,"
                + "(SELECT COUNT(co.id) FROM sys_collecon co WHERE co.targetId = cc.id AND co.optType=2 AND co.targetType=3) thumbs," +
                  "CASE WHEN ISNULL(u.avatar) THEN '"+ BaseConstants.DEFAULT_USERHEAD+"' ELSE u.avatar END avatar,";
        if(ObjectUtils.isNotEmpty(accountId)){
            sql = sql + "(SELECT COUNT(cn.id) FROM sys_collecon cn WHERE cn.targetId = cc.id AND cn.optType=2 AND cn.targetType=3 AND cn.userId = '"+accountId+"') isThumbs";
        }else{
            sql = sql + "0 isThumbs";
        }
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select(sql);
        queryWrapper.eq("targetId", targetId);
        queryWrapper.eq("replyType", replyType);
        queryWrapper.createAlias("user", "u");
        queryWrapper.orderByDesc("cc.createTime");
        List<EntityMap> list = (List<EntityMap>) pageList(queryWrapper).getRows();
        return list;
    }

    /**
     * 删除用户评论及自评论
     */
    public boolean delByUser(String commentId){
        boolean j = remove(q().eq("replyType", 2).eq("targetId", commentId));
        if(j){
            removeById(commentId);
        }
        return j;
    }

    /**
     * 查询评论详情
     */
    public EntityMap findByComment(String commentId, String accountId){
        PagerInfo pagerInfo = new PagerInfo();
        pagerInfo.setPageSize(1);
        pagerInfo.setPageNumber(1);
        String sql = "cc.id,cc.commentContent,cc.replyUserId,cc.createTime,CASE WHEN ISNULL(u.nickName) THEN '\u6e38\u5ba2' ELSE u.nickName END nickName,u.avatar,"
                + "(SELECT COUNT(co.id) FROM sys_collecon co WHERE co.targetId = cc.id AND co.optType=2 AND co.targetType=3) thumbs,";
        if(ObjectUtils.isNotEmpty(accountId)){
            sql = sql + "(SELECT COUNT(cn.id) FROM sys_collecon cn WHERE cn.targetId = cc.id AND cn.optType=2 AND cn.targetType=3 AND cn.userId = '"+accountId+"') isThumbs";
        }else{
            sql = sql + "0 isThumbs";
        }
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select(sql);
        queryWrapper.eq("cc.id", commentId);
        queryWrapper.createAlias("user", "u");
        List<EntityMap> list = (List<EntityMap>) pageList(queryWrapper).getRows();
        return list.get(0);
    }

    /**
     * 根据用户统计回复条数
     */
    public Integer countByUser(Long userId){
        return count(q().eq(true, "replyUserId", userId));
    }
}
